﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.ComponentModel;
using System.ComponentModel.Design;

using edowork.com.SQL2005;

namespace WebInvoicingsys.basecontrol.business
{
    public partial class uc_from_years : System.Web.UI.UserControl
    {
        //委托事件
        public delegate void btEvent(object sender, EventArgs e);

        public event btEvent btSelect_Click;

        private void bind()
        {
            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "select max(years) years from sys_monthly_main";
            string error_msg = "";

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            ddlist.Items.Add(new ListItem("请选所在年份", ""));

            if ((ds == null) || (ds.Tables.Count <= 0) || (ds.Tables[0].Rows.Count <= 0))
            {
                //在所有结算年份后增加一年
                ddlist.Items.Add(new ListItem(DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("yyyy")));
                ddlist.Items.Add(new ListItem(DateTime.Now.AddYears(1).ToString("yyyy"), DateTime.Now.AddYears(1).ToString("yyyy")));
            }
            else
            {

                string years = ds.Tables[0].Rows[0]["years"].ToString();

                if (String.IsNullOrEmpty(years))
                {
                    //在所有结算年份后增加一年
                    ddlist.Items.Add(new ListItem(DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("yyyy")));
                    ddlist.Items.Add(new ListItem(DateTime.Now.AddYears(1).ToString("yyyy"), DateTime.Now.AddYears(1).ToString("yyyy")));
                }
                else
                {
                    for (int i = 0; i <= 10; i++)
                    {
                        ddlist.Items.Add(new ListItem((Int32.Parse(years) + i).ToString(), (Int32.Parse(years) + i).ToString()));
                    }
                }
            }

            ddlist.SelectedIndex = 0;
        }

        public string getValue()
        {
            return ddlist.SelectedValue.ToString();
        }

        public string getText()
        {
            return ddlist.Text;
        }

        public void Reset()
        {
            ddlist.SelectedIndex = 0;
        }

        public void SetByValue(string value)
        {
            try
            {
                ddlist.ClearSelection();
                ddlist.Items.FindByValue(value).Selected = true;
            }
            catch
            {
                ddlist.ClearSelection();
                ddlist.SelectedIndex = 0;
            }
        }

        public void SetByText(string text)
        {
            try
            {
                ddlist.ClearSelection();
                ddlist.Items.FindByText(text).Selected = true;
            }
            catch
            {
                ddlist.ClearSelection();
                ddlist.SelectedIndex = 0;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }

        protected void ddlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (btSelect_Click == null)
            {
                return;
            }

            btSelect_Click(sender, e);
        }
    }
}